#define SVN_EXIST
using System;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;
#if SVN_EXIST
using  LibSubWCRev; 
#endif

namespace TUsvnAddIn
{
    
    using Extensibility;
    using EnvDTE;
    using System.Windows.Forms;
    using System.IO;
   
    /// <summary>The object for implementing an Add-in.</summary>
    /// <see also class='IDTExtensibility2' />
    public partial class Connect : IDTExtensibility2, IDTCommandTarget
    {
#if SVN_EXIST
        public SubWCRev subWc;
#endif
        private enum ShouldBeInRep
        {
            YES = 1,
            DONT_CARE,
            NO
        }

        private struct CommandsStruct
        {
            public Command _command;
            public CommandBarButton _commandBarPopupButton;
            public string _commandCaption;
            public string _commandName;
            public string _commandToolTip;
            public CommandExecFunctionDelegate _commmandFunctionP;
            public ShouldBeInRep _commandRepository;
            public int _imgID;
            public bool _beginGroup;
            public bool _cmdEnabled;
            public CommandsStruct(Command command,
                                    CommandBarButton commandBarPopupButton,
                                    string commandCaption,
                                    string commandName,
                                    string commandToolTip,
                                    CommandExecFunctionDelegate commmandFunctionP,
                                    ShouldBeInRep commandRepository,
                                    int imgID,
                                    bool beginGroup,
                                    bool cmdEnabled)
            {
                _command = command;
                _commandBarPopupButton = commandBarPopupButton;
                _commandCaption = commandCaption;
                _commandName = commandName;
                _commandToolTip = commandToolTip;
                _commmandFunctionP = commmandFunctionP;
                _commandRepository = commandRepository;
                _imgID = imgID;
                _beginGroup = beginGroup;
                _cmdEnabled = cmdEnabled;
            }
        }
        
#region CODE_WINDOW

        private CommandsStruct[] codeCommands = {   new CommandsStruct(null ,null   ,"Add"                     ,"CodeCommandAdd"                    ,"Add"                      ,cmdAdd                     ,ShouldBeInRep.NO           ,1  ,false  ,true),
                                                    /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                    new CommandsStruct(null ,null   ,"Diff"                     ,"CodeCommandDiff"                  ,"Diff"                     ,cmdDiff                    ,ShouldBeInRep.YES          ,2  ,false  ,true),
                                                    new CommandsStruct(null ,null   ,"Commit"                   ,"CodeCommandCommit"                ,"Commit"                   ,cmdCommit                  ,ShouldBeInRep.YES          ,5  ,false  ,true),
                                                    new CommandsStruct(null ,null   ,"Update"                   ,"CodeCommandUpdate"                ,"Update"                   ,cmdUpdate                  ,ShouldBeInRep.YES          ,3  ,false  ,true),
                                                    /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                    new CommandsStruct(null ,null   ,"Show Log"                 ,"CodeCommandShowLog"               ,"Show Log"                 ,cmdShowLog                 ,ShouldBeInRep.YES          ,9  ,true   ,true),
                                                    new CommandsStruct(null ,null   ,"Repro Browser"            ,"CodeCommandReproBrowser"          ,"Repro Browser"            ,cmdReproBrowser            ,ShouldBeInRep.DONT_CARE    ,7  ,false  ,true),
                                                    new CommandsStruct(null ,null   ,"Check For Modification"   ,"CodeCommandCheckForModification"  ,"Check For Modification"   ,cmdCheckForModification    ,ShouldBeInRep.YES          ,11 ,false  ,true),
                                                    new CommandsStruct(null ,null   ,"Revision graph"           ,"CodeCommandRevisionGraph"         ,"Revision graph"           ,cmdRevisionGraph           ,ShouldBeInRep.YES          ,4  ,false  ,true),
                                                    /*---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                    new CommandsStruct(null ,null   ,"Revert"                   ,"CodeCommandRevert"                ,"Revert"                   ,cmdRevert                  ,ShouldBeInRep.YES          ,6  ,true   ,true)};
#endregion

#region PROJECT_WINDOW
        private CommandsStruct[] solutionProjectCommands =  {   new CommandsStruct(null ,null   ,"Add"                      ,"SolProjCommandAdd"                    ,"Add"                      ,cmdAdd                     ,ShouldBeInRep.NO           ,1  ,false  ,true),
                                                                /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                                new CommandsStruct(null ,null   ,"Diff"                     ,"SolProjCommandDiff"                   ,"Diff"                     ,cmdDiff                    ,ShouldBeInRep.YES          ,2  ,false  ,true),
                                                                new CommandsStruct(null ,null   ,"Commit"                   ,"SolProjCommandCommit"                 ,"Commit"                   ,cmdCommit                  ,ShouldBeInRep.YES          ,5  ,false  ,true),
                                                                new CommandsStruct(null ,null   ,"Update"                   ,"SolProjCommandUpdate"                 ,"Update"                   ,cmdUpdate                  ,ShouldBeInRep.YES          ,3  ,false  ,true),
                                                                /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                                new CommandsStruct(null ,null   ,"Show Log"                 ,"SolProjCommandShowLog"                ,"Show Log"                 ,cmdShowLog                 ,ShouldBeInRep.YES          ,9  ,true   ,true),
                                                                new CommandsStruct(null ,null   ,"Repro Browser"            ,"SolProjCommandReproBrowser"           ,"Repro Browser"            ,cmdReproBrowser            ,ShouldBeInRep.DONT_CARE    ,7  ,false  ,true),
                                                                new CommandsStruct(null ,null   ,"Check For Modification"   ,"SolProjCommandCheckForModification"   ,"Check For Modification"   ,cmdCheckForModification    ,ShouldBeInRep.YES          ,11 ,false  ,true),
                                                                new CommandsStruct(null ,null   ,"Revision graph"           ,"SolProjCommandRevisionGraph"          ,"Revision graph"           ,cmdRevisionGraph           ,ShouldBeInRep.YES          ,4  ,false  ,true),
                                                                /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
                                                                new CommandsStruct(null ,null   ,"Revert"                   ,"SolProjCommandRevert"                 ,"Revert"                   ,cmdRevert                  ,ShouldBeInRep.YES          ,6  ,true   ,true),
                                                                new CommandsStruct(null ,null   ,"Clean Up"                 ,"SolProjCommandCleanUp"                ,"Clean Up"                 ,cmdCleanUp                 ,ShouldBeInRep.YES          ,12 ,false  ,true)};
#endregion

        // Variables for IDE and add-in instances
        private DTE2 applicationObject;
        private AddIn addInInstance;

        // CommandBars that will be created by the add-in
        // We must keep them at class level to remove them when the add-in is unloaded
        private CommandBarPopup CodeWindowCommandBarPopupRoot;
        private CommandBarPopup SolutionCommandBarPopupRoot;
        private CommandBarPopup ProjectCommandBarPopupRoot;


        public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
        {
            try
            {
                applicationObject = (DTE2)application;
                addInInstance = (AddIn)addInInst;
#if SVN_EXIST
                try
                {
                    subWc = new SubWCRev();
                }
                catch
                {
                    MessageBox.Show("Failed to connect to Tortoise SVN.\n\nMake sure that Tortoise SVN is installed.", "TUsvnAddIn", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
#endif

                //Prints all VS objects and their menu
                //TUsvnAddIn.Helper.TUHelper.GetCommandBarNameByControlCaption(applicationObject);

                switch(connectMode)
                {
                    case ext_ConnectMode.ext_cm_UISetup:

                        // Do nothing for this add-in with temporary user interface
                        break;

                    case ext_ConnectMode.ext_cm_Startup:

                        // The add-in was marked to load on startup
                        // Do nothing at this point because the IDE may not be fully initialized
                        // Visual Studio will call OnStartupComplete when fully initialized
                        break;

                    case ext_ConnectMode.ext_cm_AfterStartup:

                        // The add-in was loaded by hand after startup using the Add-In Manager
                        // Initialize it in the same way that when is loaded on startup
                        AddTemporaryUI();
                        break;
                }
            }
            catch(Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void OnStartupComplete(ref System.Array custom)
        {
            AddTemporaryUI();
        }

        public void AddTemporaryUI()
        {
            // Constants for names of built-in commandbars of Visual Studio
            // Constants for names of built-in commandbars of Visual Studio
            const string VS_CODE_WINDOW_COMMANDBAR_NAME = "Code Window";
            const string VS_SOLUTION_COMMANDBAR_NAME = "Solution";
            const string VS_PROJECT_COMMANDBAR_NAME = "Project";


            // Constants for names of commandbars created by the add-in
            const string CODE_WINDOWS_COMMANDBAR_POPUP_ROOT_NAME = "CodeWindowCommandBarPopupRoot";
            const string SOLUTION_COMMANDBAR_POPUP_ROOT_NAME = "SolutionCommandBarPopupRoot";
            const string PROJECT_COMMANDBAR_POPUP_ROOT_NAME = "ProjectCommandBarPopupRoot";


            // Constants for captions of commandbars created by the add-in
            const string COMMANDBAR_POPUP_ROOT = "TortoiseSVN Plug-in";

            // Built-in commandbars of Visual Studio
            CommandBar codeWindowCommandBar = null;
            CommandBar SolutionCommandBar = null;
            CommandBar ProjectCommandBar = null;

            // The collection of Visual Studio commandbars
            CommandBars commandBars = null;

            object[] CodeWindowcontextUIGuids = new object[] { };
            object[] SolutionProjectcontextUIGuids = new object[] { };


            try
            {
                for (int commandIndex = 0; commandIndex < codeCommands.Length; commandIndex++)
                {
                    addNamedCommand(ref codeCommands[commandIndex]._command, ref CodeWindowcontextUIGuids,
                                    codeCommands[commandIndex]._commandName,
                                    codeCommands[commandIndex]._commandCaption,
                                    codeCommands[commandIndex]._commandToolTip,
                                    codeCommands[commandIndex]._imgID);
                }

                for (int commandIndex = 0; commandIndex < solutionProjectCommands.Length; commandIndex++)
                {
                    addNamedCommand(ref solutionProjectCommands[commandIndex]._command, ref SolutionProjectcontextUIGuids,
                                    solutionProjectCommands[commandIndex]._commandName,
                                    solutionProjectCommands[commandIndex]._commandCaption,
                                    solutionProjectCommands[commandIndex]._commandToolTip,
                                    solutionProjectCommands[commandIndex]._imgID);
                }

                // Retrieve the collection of commandbars
                commandBars = (CommandBars)applicationObject.CommandBars;

                // Retrieve some built-in commandbars
                codeWindowCommandBar = commandBars[VS_CODE_WINDOW_COMMANDBAR_NAME];
                SolutionCommandBar = commandBars[VS_SOLUTION_COMMANDBAR_NAME];
                ProjectCommandBar = commandBars[VS_PROJECT_COMMANDBAR_NAME];

                #region "Code Window"
                // ------------------------------------------------------------------------------------
                // New sub menu under the "Code Window" menu
                // ------------------------------------------------------------------------------------

                // Add a new commandbar popup 
                CodeWindowCommandBarPopupRoot = (CommandBarPopup)codeWindowCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, 3, true);

                // Change some commandbar popup properties
                CodeWindowCommandBarPopupRoot.CommandBar.Name = CODE_WINDOWS_COMMANDBAR_POPUP_ROOT_NAME;
                CodeWindowCommandBarPopupRoot.Caption = COMMANDBAR_POPUP_ROOT;

                for (int commandIndex = 0; commandIndex < codeCommands.Length; commandIndex++)
                {
                    addControl(CodeWindowCommandBarPopupRoot, codeCommands[commandIndex]._commandBarPopupButton, codeCommands[commandIndex]._command, codeCommands[commandIndex]._commandCaption, codeCommands[commandIndex]._beginGroup, codeCommands[commandIndex]._cmdEnabled);
                }
                
                // Make visible the commandbar popup
                CodeWindowCommandBarPopupRoot.Visible = true;
                CodeWindowCommandBarPopupRoot.BeginGroup = true;
                #endregion

                #region "Solution"
                // ------------------------------------------------------------------------------------
                // New sub menu under the "Solution" menu
                // ------------------------------------------------------------------------------------

                // Add a new commandbar popup 
                SolutionCommandBarPopupRoot = (CommandBarPopup)SolutionCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, 1, true);

                // Change some commandbar popup properties
                SolutionCommandBarPopupRoot.CommandBar.Name = SOLUTION_COMMANDBAR_POPUP_ROOT_NAME;
                SolutionCommandBarPopupRoot.Caption = COMMANDBAR_POPUP_ROOT;

                for (int commandIndex = 0; commandIndex < solutionProjectCommands.Length; commandIndex++)
                {
                    addControl(SolutionCommandBarPopupRoot, solutionProjectCommands[commandIndex]._commandBarPopupButton, solutionProjectCommands[commandIndex]._command, solutionProjectCommands[commandIndex]._commandCaption, solutionProjectCommands[commandIndex]._beginGroup, solutionProjectCommands[commandIndex]._cmdEnabled);
                }

                // Make visible the commandbar popup
                SolutionCommandBarPopupRoot.Visible = true;
                SolutionCommandBarPopupRoot.BeginGroup = true;
                #endregion

                #region "Project"
                // ------------------------------------------------------------------------------------
                // New sub menu under the "Project" menu
                // ------------------------------------------------------------------------------------

                // Add a new commandbar popup 
                ProjectCommandBarPopupRoot = (CommandBarPopup)ProjectCommandBar.Controls.Add(MsoControlType.msoControlPopup, System.Type.Missing, System.Type.Missing, 1, true);

                // Change some commandbar popup properties
                ProjectCommandBarPopupRoot.CommandBar.Name = PROJECT_COMMANDBAR_POPUP_ROOT_NAME;
                ProjectCommandBarPopupRoot.Caption = COMMANDBAR_POPUP_ROOT;

                for (int commandIndex = 0; commandIndex < solutionProjectCommands.Length; commandIndex++)
                {
                    addControl(ProjectCommandBarPopupRoot, solutionProjectCommands[commandIndex]._commandBarPopupButton, solutionProjectCommands[commandIndex]._command, solutionProjectCommands[commandIndex]._commandCaption, solutionProjectCommands[commandIndex]._beginGroup, solutionProjectCommands[commandIndex]._cmdEnabled);
                }

                // Make visible the commandbar popup
                ProjectCommandBarPopupRoot.Visible = true;
                ProjectCommandBarPopupRoot.BeginGroup = true;
                #endregion
            }
            catch(System.Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void addNamedCommand(ref Command command, ref object[] contextUIGuids, string commandName, string commandCaption, string commandToolTipCaption, int imgID)
        {
            try
            {
                command = applicationObject.Commands.Item((string)(addInInstance.ProgID + "." + commandName), -1);
            }
            catch
            {
            }

            if (command == null)
            {
                command = applicationObject.Commands.AddNamedCommand(addInInstance,
                            commandName, commandCaption, commandToolTipCaption, false, imgID, ref contextUIGuids,
                            (int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
            }
        }

        private void addControl(CommandBarPopup CommandBarPopupRoot, CommandBarButton commandBarPopupButton, Command command, string commandCaption, bool beginGroup, bool supported)
        {
            // Add a new button on that commandbar popup
            commandBarPopupButton = (CommandBarButton)command.AddControl(CommandBarPopupRoot.CommandBar, CommandBarPopupRoot.Controls.Count + 1);

            // Change some button properties            
            if (supported == false)
            {
                commandCaption += " (not supported yet)";
            }

            commandBarPopupButton.Caption = commandCaption;
            commandBarPopupButton.BeginGroup = beginGroup; // create group in sub menu
        }

        public void OnDisconnection(Extensibility.ext_DisconnectMode RemoveMode, ref System.Array custom)
        {
            try
            {
                switch(RemoveMode)
                {
                    case ext_DisconnectMode.ext_dm_HostShutdown:
                    case ext_DisconnectMode.ext_dm_UserClosed:
                        if ((CodeWindowCommandBarPopupRoot != null))
                        {
                            CodeWindowCommandBarPopupRoot.Delete(true);
                        }
                        if ((SolutionCommandBarPopupRoot != null))
                        {

                            SolutionCommandBarPopupRoot.Delete(true);
                        }
                        if ((ProjectCommandBarPopupRoot != null))
                        {
                            ProjectCommandBarPopupRoot.Delete(true);
                        }
                        break;
                }
            }
            catch(System.Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        public void OnBeginShutdown(ref System.Array custom)
        {
        }

        public void OnAddInsUpdate(ref System.Array custom)
        {
        }

        public void Exec(string cmdName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;
            try
            {
                if ((executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault))
                {
                    //Code Window
                    if (cmdName.Contains("CodeCommand"))
                    {
                        
                        if (cmdName == addInInstance.ProgID + "." + "CodeCommandCheckForModification")
                        {
                            handled = true;
                            execCommand(cmdCheckForModification, new FileInfo(applicationObject.ActiveDocument.FullName).DirectoryName);
                            return;
                        }

                        if (cmdName == addInInstance.ProgID + "." + "CodeCommandReproBrowser")
                        {
                            handled = true;
                            execCommand(cmdReproBrowser, new FileInfo(applicationObject.ActiveDocument.FullName).DirectoryName);
                            return;
                        }

                        for (int commandIndex = 0; commandIndex < codeCommands.Length; commandIndex++)
                        {
                            if (cmdName == addInInstance.ProgID + "." + codeCommands[commandIndex]._commandName)
                            {
                                handled = true;
                                execCommand(codeCommands[commandIndex]._commmandFunctionP, applicationObject.ActiveDocument.FullName);
                                return;
                            }
                        }
                    }
                    else//Solution/Project
                    {
                        string FullName = getProjSolutionFullName();    
                        if (cmdName == addInInstance.ProgID + "." + "SolProjCommandCheckForModification")
                        {
                            handled = true;
                            execCommand(cmdCheckForModification, new FileInfo(FullName).DirectoryName);
                            return;
                        }

                        if (cmdName == addInInstance.ProgID + "." + "SolProjCommandReproBrowser")
                        {
                            handled = true;
                            execCommand(cmdReproBrowser, new FileInfo(FullName).DirectoryName);
                            return;
                        }

                        if (cmdName == addInInstance.ProgID + "." + "SolProjCommandCleanUp")
                        {
                            handled = true;
                            execCommand(cmdCleanUp, new FileInfo(FullName).DirectoryName);
                            return;
                        }

                        for (int commandIndex = 0; commandIndex < codeCommands.Length; commandIndex++)
                        {
                            if (cmdName == addInInstance.ProgID + "." + solutionProjectCommands[commandIndex]._commandName)
                            {
                                handled = true;
                                execCommand(solutionProjectCommands[commandIndex]._commmandFunctionP, FullName);
                                return;
                            }
                        }
                    }
                }
            }
            catch (System.Exception e)
            {
                message(e.Message);
            }
        }

        private string getProjSolutionFullName()
        {
            string name = string.Empty;
            SelectedItem selItem = applicationObject.SelectedItems.Item(1);

            if (selItem.Project != null)
            {
                name = selItem.Project.FullName;
            }
            else
            {
                name = applicationObject.Solution.FileName;
            }
            return name;
        }

        public void QueryStatus(string cmdName, vsCommandStatusTextWanted neededText, ref vsCommandStatus statusOption, ref object commandText)
        {
            if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                string FullName = string.Empty;
                ShouldBeInRep isFileInRepository = ShouldBeInRep.NO;

                if (cmdName.Contains("CodeCommand"))
                {
                    FullName = applicationObject.ActiveDocument.FullName;
                }
                else
                {
                    FullName = getProjSolutionFullName();
                }

#if SVN_EXIST
                try
                {
                    subWc.GetWCInfo(FullName, true, true);
                }
                catch
                {
                    //In case COM process was closed since other add-in was terminated.
                    subWc = new SubWCRev();
                    subWc.GetWCInfo(FullName, true, true);
                }

                if (subWc.IsSvnItem == true)
                {
                    isFileInRepository = ShouldBeInRep.YES;
                }
#endif

                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusUnsupported);
                if (cmdName.Contains("CodeCommand"))
                {
                    for (int commandIndex = 0; commandIndex < codeCommands.Length; commandIndex++)
                    {
                        if (cmdName == addInInstance.ProgID + "." + codeCommands[commandIndex]._commandName)
                        {
                            if (codeCommands[commandIndex]._cmdEnabled == false)
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusSupported); //shows command as disabled
                                return;
                            }

                            //Consider replacing the if with this:                                         
                            if (codeCommands[commandIndex]._commandRepository == ShouldBeInRep.DONT_CARE)
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported);
                                return;
                            }
                            else if ((codeCommands[commandIndex]._commandRepository == ShouldBeInRep.YES &&  isFileInRepository == ShouldBeInRep.YES) ||
                                    (codeCommands[commandIndex]._commandRepository == ShouldBeInRep.NO && isFileInRepository == ShouldBeInRep.NO))
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported);
                                return;
                            }
                            else
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusUnsupported);
                                return;
                            }
                        }
                    }
                }
                else//Solution/Project
                {
                    for (int commandIndex = 0; commandIndex < solutionProjectCommands.Length; commandIndex++)
                    {
                        if (cmdName == addInInstance.ProgID + "." + solutionProjectCommands[commandIndex]._commandName)
                        {
                            if (solutionProjectCommands[commandIndex]._cmdEnabled == false)
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusSupported); //shows command as disabled
                                return;
                            }
                                                      
                            if (solutionProjectCommands[commandIndex]._commandRepository == ShouldBeInRep.DONT_CARE)
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported);
                                return;
                            }
                            else if ((solutionProjectCommands[commandIndex]._commandRepository == ShouldBeInRep.YES && isFileInRepository == ShouldBeInRep.YES) ||
                                    (solutionProjectCommands[commandIndex]._commandRepository == ShouldBeInRep.NO && isFileInRepository == ShouldBeInRep.NO))
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusEnabled | vsCommandStatus.vsCommandStatusSupported);
                                return;
                            }
                            else
                            {
                                statusOption = (vsCommandStatus)(vsCommandStatus.vsCommandStatusUnsupported);
                                return;
                            }
                        }
                    }
                }
            }
        }

        public static void message(string msg)
        {
            MessageBox.Show(msg);
        }
    }
}

namespace TUsvnAddIn.Helper
{
    using System.Diagnostics;
    public class TUHelper
    {
        //CommandBar codeWindowBar = commandBars["Solution"];
        //CommandBar codeWindowBar = commandBars["Project"];
        //CommandBar codeWindowBar = commandBars["Code Window"];

        //CommandBar codeWindowBar = commandBars["Selection"];
        //CommandBar codeWindowBar = commandBars["Easy MDI Document Window"] -> for code tab
        //CommandBar codeWindowBar = commandBars["Control"];

        public static void GetCommandBarNameByControlCaption(DTE2 dte)
        {
            CommandBars commandBars;

            try
            {

                // The following cast is required in VS 2005 and higher because its DTE.CommandBars returns the type Object
                // (because VS 2005 and higher uses for commandbars the type Microsoft.VisualStudio.CommandBars.CommandBars 
                // of the new Microsoft.VisualStudio.CommandBars.dll assembly while VS.NET 2002/2003 used the 
                // type Microsoft.Office.Core.CommandBars of the Office.dll assembly)

                commandBars = (CommandBars)dte.CommandBars;

                foreach (CommandBar commandBar in commandBars)
                {
                    foreach (CommandBarControl commandBarControl1 in commandBar.Controls)
                    {
                        Debug.WriteLine("------------------------------------------------------------------------------------------------------------------------");
                        Debug.WriteLine("Candidate CommandBar Name: " + "\"" + commandBar.Name + "\"");
                        Debug.WriteLine("Captions for this command bar:");

                        foreach (CommandBarControl commandBarControl2 in commandBar.Controls)
                        {
                            Debug.WriteLine("  " + commandBarControl2.Caption);
                        }
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                TUsvnAddIn.Connect.message(ex.ToString());
            }
        }
    }
}